package rkw.database.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import core.communication.electionConfiguration.CMember;
import core.database.AbstractEntity;
import core.database.SexType;

@Entity
@Table(name = "member")
public class Member extends AbstractEntity {
	@Column(name = "name", length = 45)
	private String name;

	@Column(name = "surname", length = 45)
	private String surname;

	@Column(name = "address")
	private String address;

	@Column(name = "identificationField", nullable = false, unique = true, length = 45)
	// FIXME: nullable z true na false
	private String identificationField;

	@Column(name = "sex", nullable = false)
	@Enumerated(EnumType.STRING)
	private SexType sex;

	@OneToMany(mappedBy = "member", fetch = FetchType.EAGER)
	private List<AdministratorToken> tokens = new ArrayList<AdministratorToken>();

	public Member() {
		super();
	}

	public Member(String name, String surname, String address, String identificationField, SexType sex) {
		super();
		this.name = name;
		this.surname = surname;
		this.address = address;
		this.identificationField = identificationField;
		this.sex = sex;
	}

	public Member(CMember cMember) {
		super();
		this.name = cMember.getName();
		this.surname = cMember.getSurname();
		this.address = cMember.getAddress();
		this.identificationField = cMember.getIdentificationField();
		this.sex = cMember.getSex();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getIdentificationField() {
		return identificationField;
	}

	public void setIdentificationField(String identificationField) {
		this.identificationField = identificationField;
	}

	public SexType getSex() {
		return sex;
	}

	public void setSex(SexType sex) {
		this.sex = sex;
	}

	public List<AdministratorToken> getTokens() {
		return tokens;
	}

	public void setTokens(List<AdministratorToken> tokens) {
		this.tokens = tokens;
	}

	public void updateMember(CMember cMember) {
		this.name = cMember.getName();
		this.surname = cMember.getSurname();
		this.address = cMember.getAddress();
		this.identificationField = cMember.getIdentificationField();
		this.sex = cMember.getSex();
	}
	
	@Override
	public String toString() {
		return getSurname() + ", " + getName();
	}
}
